import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int strToInt(String str) {
        char [] arr=str.trim().toCharArray();
        if(arr.length==0){
            return 0;
        }
        Queue<Character> queue=new LinkedList<>();
        int max=Integer.MAX_VALUE;
        int min=Integer.MIN_VALUE;
        int flag=1;
        int i=0;
        if(arr[0]=='-'){
            flag=-1;
        }
        if(arr[0]=='-'||arr[0]=='+'){
            i=i+1;
        }
        for(;i<arr.length;i++){
            if(arr[i]>='0'&&arr[i]<='9'){
                queue.offer(arr[i]);
            }else{
                break;
            }
        }
        int answer = 0;
        int bin=max/10;
        while (!queue.isEmpty()) {
            int rst = queue.poll() - '0';
            if (answer == bin && rst > 7 || answer>bin) {
                return flag == 1 ? max : min;
            }
            answer= answer * 10 + rst;
        }
        answer=answer*flag;
        return answer;
    }
    public int[] constructArr(int[] a) {
        int len=a.length;
        if(len==0){
            return new int[0];
        }
        int[] arr=new int[len];
        arr[0]=1;
        int tmp=1;
        //下三角
        for(int i=1;i<len;i++){
            arr[i]=arr[i-1]*a[i-1];
        }
        //上三角
        for(int i=len-2;i>=0;i--){
            tmp=tmp*a[i+1];
            arr[i]=tmp*arr[i];
        }
        return arr;
    }
    //暴力破解
    public int[] constructArr1(int[] a) {
        if(a.length==0){
            return new int[0];
        }
        int len=a.length;
        int [] arr=new int[len];
        for(int i=0;i<len;i++){
            int sum=1;
            for(int j=0;j<len;j++){
                if(i==j){
                    continue;
                }else{
                   sum=sum*a[j]; 
                }
            }
            arr[i]=sum;
        }
     return arr;
    }
}